package router

import (
	"server/core/utils"

	"github.com/gin-gonic/gin"
)

type Group struct {
	Path       string
	Middleware []gin.HandlerFunc
	Func       func(r *gin.RouterGroup)
}

var defaultGroups = make([]Group, 0)
var authGroups = make([]Group, 0)

func RegisterDefaultGroup(path string, group func(r *gin.RouterGroup), middleware ...gin.HandlerFunc) {
	defaultGroups = append(defaultGroups, Group{
		Path:       path,
		Func:       group,
		Middleware: middleware,
	})

}

func RegisterAuthGroup(path string, group func(r *gin.RouterGroup), middleware ...gin.HandlerFunc) {
	authGroups = append(authGroups, Group{
		Path:       path,
		Func:       group,
		Middleware: middleware,
	})
}

func NewRouter() (router *gin.Engine) {
	gin.SetMode(gin.DebugMode)
	router = gin.Default()

	defaultGroup := router.Group("/")
	for _, group := range defaultGroups {
		g := defaultGroup.Group(group.Path, group.Middleware...)
		group.Func(g)
		utils.Info(group)
	}

	authGroup := router.Group("/", authMiddleware)
	for _, group := range authGroups {
		g := authGroup.Group(group.Path, group.Middleware...)
		utils.Info(group)
		group.Func(g)
	}

	return
}

// TODO 权限认证
func authMiddleware(c *gin.Context) {

}
